package com.resou.reader.bookdetail.comment.fragment;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.entry.BaseComment;
import com.resou.reader.api.entry.Result;
import com.resou.reader.api.service.BookService;
import com.resou.reader.base.ResouBaseFragment;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.mine.IView.UserInstance;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Author: 萤火虫
 * E-mail:  Allensuns@163.com
 * Description:
 * CreateTime: 2019-04 -10
 **/
public class FragmentCommentPresenter extends ResouBasePresenter<IFragmentCommentContract.IFragmentCommentView> implements IFragmentCommentContract.IFragmentCommentPresenter {
    BookService bookService;

    public FragmentCommentPresenter(IFragmentCommentContract.IFragmentCommentView view) {
        super(view);
        bookService = ApiImp.getInstance().getService(BookService.class);
    }


    @Override
    public void quearComment(String bookId, String type, int page) {
        mView.showProgress();
        bookService.reviewComment(bookId, type, page, pageSize)
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<BaseComment>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(BaseComment baseComment) {
                        if (page == 0) {
                            mView.getSmartRefreshLayout().finishRefresh();
                        }
                        if (baseComment.getData().size() < 15) {
                            mView.getSmartRefreshLayout().finishLoadMoreWithNoMoreData();
                        } else {
                            mView.getSmartRefreshLayout().finishLoadMore(true);
                        }
                        mView.showComment(baseComment.getData(), baseComment.getDataSize() + "");

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.getSmartRefreshLayout().finishRefresh();
                        mView.getSmartRefreshLayout().finishLoadMore(true);
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });

    }

    /**
     * 用户点赞接口
     *
     * @param status    状态
     * @param commentId 评论id
     */
    @Override
    public void remoteLikeParise(int status, int commentId) {
        mView.showProgress();
        bookService.likeComment(UserInstance.getToken(), commentId)
                .subscribeOn(getIOSchedulers())
                .observeOn(getMainSchedulers())
                .subscribe(new Observer<Result>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                    }

                    @Override
                    public void onNext(Result result) {
                        mView.likePariseSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError(e);
                    }

                    @Override
                    public void onComplete() {
                        mView.showContent();
                    }
                });

    }

}
